//
//  HWCommentViewController.m
//  JWC
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWCommentViewController.h"
#import "HWCommentTableViewCell.h"
#import "HWEssenceTool.h"
#import "MJRefresh.h"
#import "HWRecommentCell.h"

static NSString *cellID = @"comment";
@interface HWCommentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)HWEssenceTool *tool;
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSString *lastcid;

@end

@implementation HWCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"评论";
    
    [self setupHeader];
    [self setRefresh];
    [self keyboardChange];
    [self setTableView];
}

#pragma mark --- 设置表头 ---
- (void)setupHeader
{
    UIView *header = [[UIView alloc]init];
    
    HWRecommentCell *cell = [HWRecommentCell cell];
    cell.topFrame = self.frame;
    cell.size = CGSizeMake(SCREEN_WIDTH, self.frame.cellHeight);
    
    [header addSubview:cell];
    header.height = self.frame.cellHeight + 10;
    self.tableView.tableHeaderView = header;
}

#pragma mark --- 设置下拉刷新、上拉加载 ---
- (void)setRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewDatax)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreDatax)];
}

- (void)getNewDatax
{
    self.page = 1;
    [self.tool getCommentsWithID:self.frame.model.id block:^(id json, NSString *param) {
        self.modelArray = json;
        self.lastcid = param;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
    
    [self.tableView.mj_header endRefreshing];
}

- (void)getMoreDatax
{
    self.page += 1;
    [self.tool getCommentsWithID:self.frame.model.id page:self.page lastcid:self.lastcid block:^(id json, NSString *total) {
    
        for (HWCommentModel *model in json)
        {
            [self.modelArray addObject:model];
        }
        self.lastcid = total;
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark --- 监听键盘的改变 ---
- (void)keyboardChange
{
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] subscribeNext:^(NSNotification *noti) {
        //弹出时间
        CGFloat animaDuration = [noti.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        //拿到键盘弹出的frame
        CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        self.bottom.constant = SCREEN_HEIGHT - frame.origin.y;
        [UIView animateWithDuration:animaDuration animations:^{
            [self.view layoutIfNeeded];
        }];
    }];
}

// 移除通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --- 设置tableView ---
- (void)setTableView
{
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"HWCommentTableViewCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

#pragma mark --- UITableViewDelegate ---
// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWCommentModel *model = self.modelArray[indexPath.row];
    return model.cellHeight;
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建menu菜单
    UIMenuController *menu = [UIMenuController sharedMenuController];
    if (menu.isMenuVisible)
    {
        [menu setMenuVisible:NO animated:YES];
    }
    // 取出点击的cell
    HWCommentTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // 成为第一响应者
    [cell becomeFirstResponder];
    
    UIMenuItem *ding = [[UIMenuItem alloc]initWithTitle:@"顶" action:@selector(ding:)];
    UIMenuItem *copy = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copy:)];
    UIMenuItem *report = [[UIMenuItem alloc]initWithTitle:@"举报" action:@selector(report:)];
    menu.menuItems = @[ding,copy,report];
    
    CGRect cellRect = CGRectMake(0, cell.height / 2, cell.width, cell.height / 2);
    [menu setTargetRect:cellRect inView:cell];
    [menu setMenuVisible:YES animated:YES];
}

- (void)ding:(UIMenuController *)menu
{
    
}

- (void)copy:(UIMenuController *)menu
{
    
}

- (void)report:(UIMenuController *)menu
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已举报成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}

// 视图开始滚动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    //让键盘退出
    [self.view endEditing:YES];
}

// 设置表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"最新评论";
}

#pragma mark --- UITableViewDataSource ---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (HWEssenceTool *)tool{
    if (_tool == nil) {
        _tool = [[HWEssenceTool alloc]init];
        
    }
    return _tool;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
